home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / muzsrc1.zip / LAYOUT.CPP < prev    next >
C/C++ Source or Header  |  1992-07-21  |  10KB  |  239 lines

  1. // **********************************************
  2. // File: LAYOUT.CPP
  3. // Layout dialog box functions
  4.  
  5. #include "muzika.h"
  6. #include "dialog.h"
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9.  
  10. BOOL scoreDisplay = FALSE;        // Initially single part; TRUE = score
  11. int displayedPart = 0;            // The part being displayed
  12. unsigned pixelsPerStaff = 65;     // Height of a staff in pixels
  13. unsigned pixelsPerObject = 20;    // Width of an object in pixels
  14.  
  15. // ***************************************************
  16. // DialogParts is the "Layout/Parts..." dialog box function,
  17. // processing messages intended for this dialog box.
  18.  
  19. BOOL FAR PASCAL DialogParts(HWND hDlg, unsigned message, WORD wParam, LONG)
  20. {
  21.   switch (message) {
  22.     case WM_INITDIALOG:
  23.       // Process a WM_INITDIALOG message, indicating that the
  24.       // DialogParts function is called for the first time.
  25.       // The part list box is filled with names of the existing parts.
  26.       for (int index = 0; index < melody.part.number(); ++index)
  27.         SendDlgItemMessage(hDlg, ID_PARTLIST, LB_INSERTSTRING, -1,
  28.           (LONG) ((Part *) &melody.part[index])->name());
  29.       return TRUE;
  30.  
  31.     case WM_COMMAND:
  32.       // Process a WM_COMMAND message, indicating an action
  33.       // of some kind in the dialog box.
  34.       switch (wParam) {
  35.         case ID_NEWPART:
  36.           // The user selected "New":
  37.           // create a new part dialog box, using the DialogNewPart function
  38.           FARPROC lpDialog = MakeProcInstance((FARPROC) DialogNewPart, hInst);
  39.           if (DialogBox(hInst, "D_NEWPART", hDlg, lpDialog)) {
  40.             SendDlgItemMessage(hDlg, ID_PARTLIST, LB_INSERTSTRING, -1,
  41.               (LONG) ((Part *) &melody.part[melody.part.number()-1])->name());
  42.             melodyModified = TRUE;
  43.           }
  44.           FreeProcInstance(lpDialog);
  45.           break;
  46.  
  47.         case ID_REMOVEPART:
  48.           // The user selected "Remove":
  49.           // remove the selected part unless it is being displayed,
  50.           // or is the last one.
  51.           int currSel = (int) SendDlgItemMessage(hDlg, ID_PARTLIST,
  52.             LB_GETCURSEL, 0, 0);
  53.           if (currSel != LB_ERR)
  54.             if (SendDlgItemMessage(hDlg, ID_PARTLIST, LB_GETCOUNT, 0, 0) > 1)
  55.               if (displayedPart != currSel) {
  56.                 // See if the removed part contains anything
  57.                 if (((Part *) &melody.part[currSel])->staff.number())
  58.                   if (MessageBox(hDlg, "Part is not empty. Remove anyway?",
  59.                      "WARNING", MB_ICONEXCLAMATION | MB_YESNOCANCEL) != IDYES)
  60.                      return TRUE;
  61.                 melody.part.destroyAt(currSel);
  62.                 SendDlgItemMessage(hDlg, ID_PARTLIST,
  63.                   LB_DELETESTRING, currSel, 0);
  64.                 if (displayedPart > currSel)
  65.                   --displayedPart;
  66.                 melodyModified = TRUE;
  67.               }
  68.               else
  69.                 // The selected part is the one being displayed
  70.                 MessageBox(hDlg, "Cannot remove the part being displayed",
  71.                   NULL, MB_ICONSTOP | MB_OK);
  72.             else
  73.               // The selected part is the last one left
  74.               MessageBox(hDlg, "Cannot remove the last part", NULL,
  75.                 MB_ICONSTOP | MB_OK);
  76.           else
  77.             // No selection has been made in the list box
  78.             MessageBox(hDlg, "No part is currently selected", NULL,
  79.               MB_ICONEXCLAMATION | MB_OK);
  80.           break;
  81.  
  82.         case IDOK:
  83.           // The user selected OK:
  84.           // finish the dialog box
  85.           EndDialog(hDlg, TRUE);
  86.           break;
  87.       }
  88.       return TRUE;
  89.   }
  90.  
  91.   return FALSE;
  92. }
  93.  
  94. // **********************************************
  95. // DialogPage is the "Layout/Page..." dialog box function,
  96. // processing messages intended for this dialog box.
  97.  
  98. BOOL FAR PASCAL DialogPage(HWND hDlg, unsigned message, WORD wParam, LONG lParam)
  99. {
  100.   switch (message) {
  101.     case WM_INITDIALOG:
  102.       // Process a WM_INITDIALOG message, indicating that the
  103.       // DialogPage function is called for the first time.
  104.       // Various controls of the dialog box are initialized.
  105.       SendDlgItemMessage(hDlg, scoreDisplay ? ID_SCORE : ID_SINGLEPART,
  106.         BM_SETCHECK, 1, 0);
  107.       for (int index = 0; index < melody.part.number(); ++index)
  108.         SendDlgItemMessage(hDlg, ID_DISPLAYEDPART, CB_INSERTSTRING, -1,
  109.           (LONG) ((Part *) &melody.part[index])->name());
  110.       SendDlgItemMessage(hDlg, ID_DISPLAYEDPART,
  111.         CB_SETCURSEL, displayedPart, 0);
  112.       char numstring[4];
  113.       SetDlgItemText(hDlg, ID_STAFFHEIGHT,
  114.         itoa(pixelsPerStaff, numstring, 10));
  115.       SetDlgItemText(hDlg, ID_OBJECTWIDTH,
  116.         itoa(pixelsPerObject, numstring, 10));
  117.       return TRUE;
  118.  
  119.     case WM_COMMAND:
  120.       // Process a WM_COMMAND message, indicating an action
  121.       // of some kind in the dialog box.
  122.       switch (wParam) {
  123.         case ID_SINGLEPART:
  124.         case ID_SCORE:
  125.           // The user has selected one of the display radio buttons:
  126.           // check the appropriate button
  127.           if (HIWORD(lParam) == BN_CLICKED)
  128.             EnableWindow(GetDlgItem(hDlg, ID_DISPLAYEDPART),
  129.               (wParam != ID_SCORE));
  130.           return TRUE;
  131.  
  132.         case IDOK:
  133.           // The user selected OK:
  134.           // read the staff height and object width from the
  135.           // edit controls, and update the current display
  136.           scoreDisplay = (int) SendDlgItemMessage(hDlg, ID_SCORE,
  137.             BM_GETCHECK, 0, 0);
  138.           displayedPart = (int) SendDlgItemMessage(hDlg, ID_DISPLAYEDPART,
  139.             CB_GETCURSEL, 0, 0);
  140.  
  141.           // Read the numerical values (staff height and object width)
  142.           BOOL translated;
  143.           int temp = GetDlgItemInt(hDlg, ID_STAFFHEIGHT, &translated, 0);
  144.           if (temp >= 40) {
  145.             pixelsPerStaff = temp;
  146.             EndDialog(hDlg, TRUE);
  147.           }
  148.           else {
  149.             MessageBox(hDlg, "Must have at least 40 pixels per staff", NULL,
  150.               MB_ICONEXCLAMATION | MB_OK);
  151.             SetDlgItemText(hDlg, ID_STAFFHEIGHT,
  152.               itoa(pixelsPerStaff, numstring, 10));
  153.           }
  154.           temp = GetDlgItemInt(hDlg, ID_OBJECTWIDTH, &translated, 0);
  155.           pixelsPerObject = temp;
  156.  
  157.           // Initialize the display parameters if necessary
  158.           if (scoreDisplay) {
  159.             // Calculate the score display parameters
  160.             scoreFirstStaff = scoreFirstPart = 0;
  161.             scoreMultiplicity = scoreStaves = 0;
  162.             for (int index = 0; index < melody.part.number(); ++index) {
  163.               Part &p = *((Part *) &melody.part[index]);
  164.               scoreMultiplicity += p.multiplicity();
  165.               if (p.staff.number()/p.multiplicity() > scoreStaves)
  166.                 scoreStaves = p.staff.number()/p.multiplicity();
  167.             }
  168.             SetScrollRange(hEditWnd, SB_VERT, 0, scoreStaves-1, TRUE);
  169.             SetScrollPos(hEditWnd, SB_VERT, 0, TRUE);
  170.           }
  171.           else {
  172.             // Prepare the scroll bar for the single part display
  173.             Part &p = *((Part *) &melody.part[displayedPart]);
  174.             SetScrollRange(hEditWnd, SB_VERT, 0, p.staff.number() ?
  175.               ((Staff *) &p.staff[p.staff.number()-1])->Y() : 0, TRUE);
  176.             SetScrollPos(hEditWnd, SB_VERT, p.GetPartY(), TRUE);
  177.           }
  178.  
  179.           // Refresh the screen
  180.           InvalidateRect(hEditWnd, NULL, TRUE);
  181.           return TRUE;
  182.       }
  183.       return TRUE;
  184.   }
  185.  
  186.   return FALSE;
  187. }
  188.  
  189. // **********************************************
  190. // DialogNewPart is the "Layout/Part.../New..." dialog box function,
  191. // processing messages intended for this dialog box.
  192.  
  193. BOOL FAR PASCAL DialogNewPart(HWND hDlg, unsigned message, WORD wParam, LONG)
  194. {
  195.   char partName[MAXPARTNAME+1];
  196.  
  197.   switch(message) {
  198.     case WM_INITDIALOG:
  199.       // Process a WM_INITDIALOG message, indicating that the
  200.       // DialogNewPart function is called for the first time.
  201.       // The dialog box fields are initialized.
  202.       SendDlgItemMessage(hDlg, ID_NEWPARTNAME, EM_LIMITTEXT, MAXPARTNAME, 0);
  203.       SetDlgItemText(hDlg, ID_NEWPARTMULTIPLICITY, "1");
  204.       return TRUE;
  205.  
  206.     case WM_COMMAND:
  207.       // Process a WM_COMMAND, indicating that the user has selected
  208.       // one of the finishing buttons (OK or CANCEL).
  209.       if (wParam == IDOK) {
  210.         // The user selected OK:
  211.         // create the new part
  212.         int newIndex = melody.part.number();
  213.         BOOL translated;
  214.         int mult = GetDlgItemInt(hDlg, ID_NEWPARTMULTIPLICITY, &translated, 0);
  215.         if (mult >= 1 && mult <= MAXMULTIPLICITY) {
  216.           // Multiplicity is within range:
  217.           // create a new part and insert it in the part list
  218.           if (GetDlgItemText(hDlg, ID_NEWPARTNAME, partName, MAXPARTNAME))
  219.             melody.part.insertAt(*new Part(partName, mult), newIndex);
  220.           EndDialog(hDlg, TRUE);
  221.         }
  222.         else {
  223.           // Multiplicity entered by user was illegal
  224.           char numstr[40];
  225.           sprintf(numstr, "Staff multiplicity cannot exceed %d", MAXMULTIPLICITY);
  226.           MessageBox(hDlg, numstr, NULL, MB_ICONEXCLAMATION | MB_OK);
  227.         }
  228.       }
  229.  
  230.       if (wParam == IDCANCEL)
  231.         // The user selected CANCEL:
  232.         // just exit from the dialog box, without creating any part.
  233.         EndDialog(hDlg, FALSE);
  234.       return TRUE;
  235.   }
  236.  
  237.   return FALSE;
  238. }
  239.